home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / numbrw.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  17.3 KB  |  512 lines

  1. #ifndef __STD_RW_LOCNUMRW__
  2. #define __STD_RW_LOCNUMRW__
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /****************************************************************************
  6.  *
  7.  * rw/numbrw - Internal classes for numeric formatting and parsing.
  8.  *
  9.  * $Id: numbrw,v 1.19 1996/10/22 05:12:47 delaney Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  14.  * ALL RIGHTS RESERVED *
  15.  * The software and information contained herein are proprietary to, and
  16.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  17.  * intends to preserve as trade secrets such software and information.
  18.  * This software is furnished pursuant to a written license agreement and
  19.  * may be used, copied, transmitted, and stored only in accordance with
  20.  * the terms of such license and with the inclusion of the above copyright
  21.  * notice.  This software and information or any other copies thereof may
  22.  * not be provided or otherwise made available to any other person.
  23.  *
  24.  * Notwithstanding any other lease or license that may pertain to, or
  25.  * accompany the delivery of, this computer software and information, the
  26.  * rights of the Government regarding its use, reproduction and disclosure
  27.  * are as set forth in Section 52.227-19 of the FARS Computer
  28.  * Software-Restricted Rights clause.
  29.  *
  30.  * Use, duplication, or disclosure by the Government is subject to
  31.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  32.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  33.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  34.  * P.O. Box 2328, Corvallis, Oregon 97339.
  35.  *
  36.  * This computer software and information is distributed with "restricted
  37.  * rights."  Use, duplication or disclosure is subject to restrictions as
  38.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  39.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  40.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  41.  * then the "Alternate III" clause applies.
  42.  *
  43.  ***************************************************************************/
  44.  
  45.  
  46. #ifndef _RWSTD_NO_NAMESPACE
  47. namespace __rwstd {
  48. #endif
  49.  
  50.  
  51. // -------------------------------------------------------
  52. // Implementation class template -- timepunct_init<charT>.
  53. // -------------------------------------------------------
  54.  
  55. // Structure used to initialize the private data members of timepunct<charT>.
  56.  
  57. template <class charT>
  58. class _RWSTDExportTemplate timepunct_init {
  59.  public:
  60.   const charT *dn_[7][2];       // Day names and abbreviations
  61.   const charT *mn_[12][2];      // Month names and abbreviations
  62.   const charT *ampm_[2];        // AM/PM strings
  63.   const charT *bad_;            // Bad value output signal
  64.   const charT *ord_[100];       // Ordinal numbers
  65.   const charT *pat_[6];         // Patterns for formats "xXcDrT" resp.
  66. };
  67.  
  68. // -------------------------------------------------------
  69. // Implementation class template -- timepunct_data<charT>.
  70. // -------------------------------------------------------
  71.  
  72. // timepunct<charT> derives from this (via rwstd::timepunct_impl) to get its
  73. // private data members.
  74.  
  75. template <class charT>
  76. class _RWSTDExportTemplate timepunct_data {
  77.   friend class timepunct<charT>;
  78.   friend class keyword_cracker<charT>;
  79.  public:
  80.   typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
  81.  private:
  82.  
  83.   string_type dn_[7][2];
  84.   string_type mn_[12][2];
  85.   string_type ampm_[2];
  86.   string_type bad_;
  87.   string_type ord_[100];
  88.   string_type pat_[6];
  89.  
  90.   keyword_def<charT> dn_defs_[7][2];
  91.   keyword_map<charT> dn_map_;
  92.   keyword_def<charT> mn_defs_[12][2];
  93.   keyword_map<charT> mn_map_;
  94.  
  95.  protected:
  96.   timepunct_data (const timepunct_init<charT> &i);
  97.   void rw_init (void);
  98. };
  99.  
  100. // -------------------------------------------------------
  101. // Implementation class template -- timepunct_impl<charT>.
  102. // -------------------------------------------------------
  103.  
  104. // timepunct<charT> derives from this to get the parts of its behavior that
  105. // must be specialized on the character type.  Currently the only specialized
  106. // behavior is the initialization logic in the constructor.
  107.  
  108. template <class charT>
  109. class _RWSTDExportTemplate timepunct_impl :
  110.     public timepunct_data<charT>
  111. {
  112.  protected:
  113.   timepunct_impl(void);
  114. };
  115.  
  116. _RWSTD_TEMPLATE
  117. class _RWSTDExport timepunct_impl<char>:                 // Specialization
  118.     public timepunct_data<char>
  119. {
  120.   static timepunct_init<char> ivals_;       // Vendor-supplied
  121.  protected:
  122.   timepunct_impl (void):
  123.       timepunct_data<char>(ivals_) { }
  124. };
  125.  
  126. #ifndef _RWSTD_NO_WIDE_CHAR
  127. _RWSTD_TEMPLATE
  128. class _RWSTDExport timepunct_impl<wchar_t>:              // Specialization
  129.     public timepunct_data<wchar_t>
  130. {
  131.   static timepunct_init<wchar_t> ivals_;    // Vendor-supplied
  132.  protected:
  133.   timepunct_impl(void):
  134.       timepunct_data<wchar_t>(ivals_) { }
  135. };
  136. #endif // _RWSTD_NO_WIDE_CHAR
  137.  
  138.  
  139. // ----------------------------------------------------
  140. // Time and date punctuation facet -- timepunct<charT>.
  141. // ----------------------------------------------------
  142.  
  143. template <class charT>
  144. class timepunct : public locale::facet,
  145.     public timepunct_impl<charT>
  146. {
  147.  public:
  148.   typedef charT char_type;
  149.   typedef basic_string<charT,char_traits<charT>,allocator<charT> >
  150.     string_type;
  151.  
  152.   _EXPLICIT timepunct (size_t refs=0):
  153.       locale::facet(refs,locale::time) { }
  154.  
  155.   string_type dayname (int day, bool abbreviated) const
  156.       { return do_dayname(day,abbreviated); }
  157.   string_type monthname (int month, bool abbreviated) const
  158.       { return do_monthname(month,abbreviated); }
  159.   string_type ordinal (int number) const
  160.       { return do_ordinal(number); }
  161.  
  162.   static locale::id id;
  163.  
  164.   // Implementation:
  165.   enum { facet_cat_ = locale::time, ok_implicit_ = 1 };
  166.  
  167.  protected:
  168.   virtual ~timepunct() { }
  169.  
  170.   virtual string_type do_dayname (int day, bool abbreviated) const;
  171.   virtual string_type do_monthname (int month, bool abbreviated) const;
  172.   virtual string_type do_ordinal (int number) const;
  173.  
  174.  private:
  175.   void rw_init (void);
  176.  
  177.   #ifdef _RWSTD_NO_MEMBER_TEMPLATES
  178.   locale::id &get_id (void) const { return id; }
  179.   #endif
  180. };
  181.  
  182.  
  183. // --------------------------------------------------------
  184. // Implementation class template -- keyword_cracker<charT>.
  185. // --------------------------------------------------------
  186.  
  187. // Gives derived classes a backdoor way to get the private keyword_map and
  188. // other members out of numpunct and timepunct facets.
  189.  
  190. template <class charT>
  191. class _RWSTDExportTemplate keyword_cracker {
  192.  public:
  193.   static const keyword_map<charT>& get_tf_map
  194.       (const numpunct<charT> &nump)
  195.       { return nump.tf_map_; }
  196.   static const keyword_map<charT>& get_day_map
  197.       (const timepunct<charT>& timp)
  198.       { return timp.dn_map_; }
  199.   static const keyword_map<charT>& get_month_map
  200.       (const timepunct<charT>& timp)
  201.       { return timp.mn_map_; }
  202.  
  203.   static const _TYPENAME numpunct<charT>::string_type& get_tf_string
  204.       (const numpunct<charT>& nump,bool v)
  205.       { return v? nump.tn_ : nump.fn_; }
  206.   static const _TYPENAME timepunct<charT>::string_type&
  207.       get_day_string
  208.       (const timepunct<charT>& timp,unsigned n,bool abbr)
  209.       { return n<7? timp.dn_[n][abbr] : timp.bad_; }
  210.   static const _TYPENAME timepunct<charT>::string_type&
  211.       get_month_string
  212.       (const timepunct<charT>& timp,unsigned n,bool abbr)
  213.       { return n<12? timp.mn_[n][abbr] : timp.bad_; }
  214.   static const _TYPENAME timepunct<charT>::string_type&
  215.       get_ampm_string
  216.       (const timepunct<charT>& timp,bool pm)
  217.       { return timp.ampm_[pm]; }
  218.   static const _TYPENAME timepunct<charT>::string_type&
  219.       get_ord_string
  220.       (const timepunct<charT>& timp,unsigned n)
  221.       { return n<99? timp.ord_[n] : timp.bad_; }
  222.   static const _TYPENAME timepunct<charT>::string_type&
  223.       get_patt_string
  224.       (const timepunct<charT>& timp,unsigned n)
  225.       { return n<6? timp.pat_[n] : timp.bad_; }
  226. };
  227.  
  228. // -------------------------------------------------------------
  229. // Implementation class template -- digit_handler_base_1<charT>.
  230. // -------------------------------------------------------------
  231.  
  232. // Contains charT-dependent functionality that is common to the digit_reader
  233. // and digit_writer classes, particularly related to getting access to ctype
  234. // and punct facets they both use.
  235.  
  236. template <class charT>
  237. class _RWSTDExportTemplate digit_handler_base_1:
  238.     public keyword_cracker<charT>
  239. {
  240.  public:
  241.   const ctype<charT> &ctyp;
  242.   const digit_map<charT>& dmap;
  243.   const punct_data<charT>& punct;
  244.  
  245.   // Constructor for general numerics, gets punct from loc's numpunct facet.
  246.   digit_handler_base_1 (const locale &loc);
  247.  
  248.   // Constructor for money, passed punct_data is in fact a moneypunct facet.
  249.   digit_handler_base_1 (const locale &loc,const punct_data<charT>&);
  250. };
  251.  
  252. // ------------------------------------------
  253. // Implementation class -- digit_reader_base.
  254. // ------------------------------------------
  255.  
  256. // Contains digit_reader members that do not depend on the digit_reader
  257. // template parameters, particularly for converting strings of char digits
  258. // to long and floating-point binary values.
  259.  
  260. class _RWSTDExport digit_reader_base : public digit_map_base
  261. {
  262.  public:
  263.   enum {
  264.     good              = 0x0000,
  265.     no_digits         = 0x0001,
  266.     too_many_digits   = 0x0002,
  267.     too_many_groups   = 0x0004,
  268.     group_too_long    = 0x0008,
  269.     group_is_empty    = 0x0010,
  270.     bad_grouping      = 0x0020,
  271.     bad_exponent      = 0x0040,
  272.     overflow          = 0x0080,
  273.     bad_curr_symbol   = 0x0100,
  274.     bad_sign          = 0x0200
  275.   };
  276.  
  277.   enum {
  278.     digit_buf_size = LDBL_MAX_10_EXP+200
  279.   };
  280.  
  281.   // Fields defining digits to be converted, usually filled in by derived
  282.   // class before calling one of the conversion methods below.
  283.   int error;                    // Bit-field
  284.   int radix;                    // 8, 10, or 16, 0=not yet determined
  285.   bool is_signed;               // Sign allowed in input
  286.   bool negative;                // Minus sign was present
  287.   bool advanced;                // Iterator was advanced
  288.   bool reached_end;             // Needed a char, but iterator matched end
  289.   bool exp_negative;            // Minus sign was present on exponent
  290.   char *frac_beg;               // Start of fractional part of digits
  291.   char *exp_beg;                // Start of exponent in digits
  292.   char digits[digit_buf_size];  // Binary digits (i.e. 0,1,2 not '0','1','2')
  293.  
  294.   digit_reader_base (void): error(good), radix(0),
  295.       is_signed(true), negative(false), advanced(false), reached_end(false)
  296.   { }
  297.  
  298.   unsigned long to_ulong (char *end);
  299.   unsigned long to_ulong_dec (char *beg,char *end);
  300.  
  301. #ifdef _RWSTD_LONG_LONG
  302.   unsigned _RWSTD_LONG_LONG to_ulong_ulong (char *end);
  303. #endif
  304.  
  305.   void *to_pointer (char *end);
  306.   float to_float (char *end);
  307.   double to_double (char *end);
  308.   long double to_long_double (char *end);
  309.  
  310.  private:
  311.   void to_floating_point (char *end, char *pattern, void *result);
  312. };
  313.  
  314. // ------------------------------------------------------------
  315. // Implementation class template -- digit_reader_base_1<charT>.
  316. // ------------------------------------------------------------
  317.  
  318. // Contains parts of digit_reader that depend on charT but not on the iterator
  319. // type.
  320.  
  321. template <class charT>
  322. class _RWSTDExportTemplate digit_reader_base_1 :
  323.     public digit_reader_base,
  324.     public digit_handler_base_1<charT>
  325. {
  326.  public:
  327.   typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
  328.  
  329.  protected:
  330.   // Constructor for general numerics, gets punct from loc's numpunct<charT>.
  331.   digit_reader_base_1 (const locale &loc);
  332.  
  333.   // Constructor for use by derived money_reader, caller passes punct, which
  334.   // in fact is loc's moneypunct<charT,true|false> facet.
  335.   digit_reader_base_1 (const locale &loc, const punct_data<charT>&);
  336.  
  337.   // These could be moved to digit_handler_base_1<charT> ...
  338.   charT get_decimal_point() const { return this->punct.dp_; }
  339.   charT get_thousands_sep() const { return this->punct.ts_; }
  340.   const char *get_grouping() const { return this->punct.gr_.c_str(); }
  341. };
  342.  
  343. // -------------------------------------------------------------------
  344. // Implementation class template -- digit_reader<charT,InputIterator>.
  345. // -------------------------------------------------------------------
  346.  
  347. // Facets that do numeric input use temporary objects of this class to
  348. // extract keywords and strings of digits and punctuation from the input
  349. // stream.
  350.  
  351. template <class charT,class InputIterator>
  352. class _RWSTDExportTemplate digit_reader :
  353.     public digit_reader_base_1<charT>
  354. {
  355.  public:
  356.   // Constructor for reading general numerics.
  357.   digit_reader (InputIterator& in,InputIterator& end,
  358.       ios_base &io);
  359.  
  360.  protected:
  361.   // Constructor for use by derived money_reader class.
  362.   digit_reader (InputIterator& in,InputIterator& end,
  363.       ios_base &io,const punct_data<charT>&);
  364.  
  365.  public:
  366.   char *get_int_digits (void);      // Get integer-format digits
  367.   char *get_float_digits (void);    // Get floating point-format digits
  368.   char *get_pointer_digits (void);  // Get pointer-format digits
  369.  
  370.   // Parse keyword from input, return associated integer value
  371.   int get_keyword (const keyword_map<charT>&);
  372.  
  373.  protected:
  374.   ios_base &io;
  375.   InputIterator& in;
  376.   InputIterator& end;
  377.  
  378.   const char *grouping;
  379.   charT thousands_sep;
  380.   char groups[40];
  381.   char *gpos;
  382.  
  383.   // Helper functions
  384.   char *get_digit_string (char*);       // Get a string of digits
  385.   char *get_digit_groups (char*);       // Get grouped digits (recursive)
  386.  
  387.   inline bool at_end (void) {           // Test for end of stream, and
  388.     if (in==end) {                      //  remember whether end was
  389.       this->reached_end=true;           //  ever reached
  390.       return true;
  391.     }
  392.     return false;
  393.   }
  394. };
  395.  
  396.  
  397. // ------------------------------------------
  398. // Implementation class -- digit_writer_base.
  399. // ------------------------------------------
  400.  
  401. // Contains the parts of digit_writer that do not depend on the template
  402. // parameters.
  403.  
  404. class _RWSTDExportTemplate digit_writer_base :
  405.     public digit_map_base
  406. {
  407.  public:
  408.   digit_writer_base (ios_base &b);
  409.  
  410.   void get_pattern(char *p, char *prefix, bool is_signed);
  411.   bool get_f_pattern(char *p, char prefix);
  412.   void digitize (void*);
  413.   void digitize (unsigned short);
  414.   void digitize (short);
  415.   void digitize (unsigned int);
  416.   void digitize (int);
  417.   void digitize (unsigned long);
  418.   void digitize (long);
  419. #ifdef _RWSTD_LONG_LONG
  420.   void digitize (unsigned _RWSTD_LONG_LONG);
  421.   void digitize (_RWSTD_LONG_LONG);
  422. #endif
  423.   void digitize (double);
  424. #ifndef _RWSTD_NO_LONG_DOUBLE
  425.   void digitize (long double);
  426. #endif
  427.  
  428.   ios_base &io;
  429.   ios_base::fmtflags flags;
  430.   int precision,width,radix,iprecision,num_groups;
  431.   enum { left, internal, right } adjust;
  432.   bool fractional,separable;
  433.   char *start,*end,*group;
  434.   char buffer[400];
  435.   char groups[150];
  436.  
  437.   int calc_groups (int,const char*);
  438. };
  439.  
  440. // ------------------------------------------------------------
  441. // Implementation class template -- digit_writer_base_1<charT>.
  442. // ------------------------------------------------------------
  443.  
  444. // Contains parts of digit_writer<charT,OutputWriter> that depend only on the
  445. // charT parameter.
  446.  
  447. template <class charT>
  448. class _RWSTDExportTemplate digit_writer_base_1 :
  449.     public digit_writer_base,
  450.     public digit_handler_base_1<charT>
  451. {
  452.  public:
  453.   typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
  454.  
  455.  protected:
  456.   // Constructor for general numerics, gets punct from loc's numpunct<charT>.
  457.   digit_writer_base_1
  458.       (ios_base&,const locale&);
  459.  
  460.   // Constructor for money, passed punct is really a moneypunct<charT,Intl>.
  461.   digit_writer_base_1  (ios_base&,const punct_data<charT>&);
  462.  
  463.   // These could be moved to digit_handler_base_1<charT> ...
  464.   charT get_decimal_point() const { return this->punct.dp_; }
  465.   charT get_thousands_sep() const { return this->punct.ts_; }
  466.   const char *get_grouping() const { return this->punct.gr_.c_str(); }
  467. };
  468.  
  469. // --------------------------------------------------------------------
  470. // Implementation class template -- digit_writer<charT,OutputIterator>.
  471. // --------------------------------------------------------------------
  472.  
  473. // Facets that do numeric output use temporary objects of this type (on the
  474. // stack) to convert numeric quantities into sequences of digits and other
  475. // punctuation.
  476.  
  477. template <class charT,class OutputIterator>
  478. class _RWSTDExportTemplate digit_writer :
  479.     public digit_writer_base_1<charT>
  480. {
  481.  public:
  482.   typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
  483.  
  484.   // Constructor for general numerics.
  485.   digit_writer (OutputIterator&,ios_base&);
  486.  
  487.  protected:
  488.   // Constructor for use by derived money_writer classes, passed punct_data is
  489.   // really a moneypunct<charT,Intl> facet.
  490.   digit_writer (OutputIterator&,ios_base&,
  491.       const punct_data<charT>&);
  492.  
  493.  public:
  494.   void put_digits (charT fill);
  495.   void put_keyword (const string_type&,charT fill);
  496.  
  497.  protected:
  498.   OutputIterator &out;
  499. };
  500.  
  501.  
  502. #ifndef _RWSTD_NO_NAMESPACE
  503. } // namespace __rwstd
  504. #endif
  505.  
  506. #ifdef _RWSTD_COMPILE_INSTANTIATE
  507. #include <rw/numbrw.cc>
  508. #endif
  509.  
  510. #pragma option pop
  511. #endif // __STD_RW_LOCNUMRW__
  512.